
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>代码分隔 · React 基础学习资料</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="小翼">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../chapter06/00.html" />
    
    
    <link rel="prev" href="07.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    React 手稿
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter01/00.html">
            
                <a href="../chapter01/00.html">
            
                    
                    基础知识
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter01/01.html">
            
                <a href="../chapter01/01.html">
            
                    
                    初识React
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter01/02.html">
            
                <a href="../chapter01/02.html">
            
                    
                    React 特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter01/03.html">
            
                <a href="../chapter01/03.html">
            
                    
                    React Initial Project
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter01/04.html">
            
                <a href="../chapter01/04.html">
            
                    
                    Component和Dom
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../chapter01/05.html">
            
                <a href="../chapter01/05.html">
            
                    
                    生态组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter02/00.html">
            
                <a href="../chapter02/00.html">
            
                    
                    组件生命周期
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter02/01.html">
            
                <a href="../chapter02/01.html">
            
                    
                    装载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter02/02.html">
            
                <a href="../chapter02/02.html">
            
                    
                    更新
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter02/03.html">
            
                <a href="../chapter02/03.html">
            
                    
                    卸载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter02/04.html">
            
                <a href="../chapter02/04.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter02/05.html">
            
                <a href="../chapter02/05.html">
            
                    
                    React.Component
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter03/00.html">
            
                <a href="../chapter03/00.html">
            
                    
                    state
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter03/01.html">
            
                <a href="../chapter03/01.html">
            
                    
                    Component state
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter03/02.html">
            
                <a href="../chapter03/02.html">
            
                    
                    React-Redux
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter03/03.html">
            
                <a href="../chapter03/03.html">
            
                    
                    React-Router
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter03/04.html">
            
                <a href="../chapter03/04.html">
            
                    
                    redux-saga
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter04/00.html">
            
                <a href="../chapter04/00.html">
            
                    
                    组件其他方法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter04/01.html">
            
                <a href="../chapter04/01.html">
            
                    
                    其他API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter04/02.html">
            
                <a href="../chapter04/02.html">
            
                    
                    类属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter04/03.html">
            
                <a href="../chapter04/03.html">
            
                    
                    实例属性
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="00.html">
            
                <a href="00.html">
            
                    
                    React 高级用法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="01.html">
            
                <a href="01.html">
            
                    
                    keys
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="02.html">
            
                <a href="02.html">
            
                    
                    事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="03.html">
            
                <a href="03.html">
            
                    
                    Refs
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="04.html">
            
                <a href="04.html">
            
                    
                    Fragment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="05.html">
            
                <a href="05.html">
            
                    
                    Context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="06.html">
            
                <a href="06.html">
            
                    
                    高阶组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="07.html">
            
                <a href="07.html">
            
                    
                    类型查检
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.6.8" data-path="08.html">
            
                <a href="08.html">
            
                    
                    代码分隔
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter06/00.html">
            
                <a href="../chapter06/00.html">
            
                    
                    React 实例
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter06/01.html">
            
                <a href="../chapter06/01.html">
            
                    
                    List
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter06/02.html">
            
                <a href="../chapter06/02.html">
            
                    
                    Form
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter06/03.html">
            
                <a href="../chapter06/03.html">
            
                    
                    Form高级
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter06/04.html">
            
                <a href="../chapter06/04.html">
            
                    
                    Tooltip
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter07/00.html">
            
                <a href="../chapter07/00.html">
            
                    
                    React项目打包部署
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter07/01.html">
            
                <a href="../chapter07/01.html">
            
                    
                    与服务端交互
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter07/02.html">
            
                <a href="../chapter07/02.html">
            
                    
                    打包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter07/03.html">
            
                <a href="../chapter07/03.html">
            
                    
                    部署
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter08/00.html">
            
                <a href="../chapter08/00.html">
            
                    
                    浅谈ES
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter08/01.html">
            
                <a href="../chapter08/01.html">
            
                    
                    ES6 (ES2015)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter08/02.html">
            
                <a href="../chapter08/02.html">
            
                    
                    ES7 (ES2016)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter08/03.html">
            
                <a href="../chapter08/03.html">
            
                    
                    ES8 (ES2017)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter08/04.html">
            
                <a href="../chapter08/04.html">
            
                    
                    ES9 (ES2018)
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter09/00.html">
            
                <a href="../chapter09/00.html">
            
                    
                    React Hooks
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter09/01.html">
            
                <a href="../chapter09/01.html">
            
                    
                    State Hooks useState
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter09/02.html">
            
                <a href="../chapter09/02.html">
            
                    
                    State Hooks useReducer
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >代码分隔</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x4EE3;&#x7801;&#x5206;&#x9694;">&#x4EE3;&#x7801;&#x5206;&#x9694;</h1>
<p>&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x5927;&#x591A;&#x6570;<code>React</code>&#x9879;&#x76EE;&#x90FD;&#x662F;&#x4EE5;<code>Webpack</code> &#x6216;&#x8005; <code>Browserify</code>&#x7B49;&#x5C06;&#x4E00;&#x5806;&#x7684;jsx&#x6587;&#x4EF6;&#x7EC4;&#x7EC7;&#x4E00;&#x8D77;&#xFF0C;&#x5E76;&#x4E14;&#x7531;&#x4E00;&#x4E2A;&#x7C7B;&#x4F3C;<code>index.js</code>&#x7684;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#x4E32;&#x8054;&#x8D77;&#x6765;&#x7684;&#x5355;&#x9875;&#x9762;<code>web</code>&#x9875;&#x9762;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-comment">// math.js</span>
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span>(<span class="hljs-params">a, b</span>) </span>{
  <span class="hljs-keyword">return</span> a + b;
}
</code></pre>
<p>App:</p>
<pre><code class="lang-js"><span class="hljs-comment">// app.js</span>
<span class="hljs-keyword">import</span> { add } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./math.js&apos;</span>;

<span class="hljs-built_in">console</span>.log(add(<span class="hljs-number">16</span>, <span class="hljs-number">26</span>)); <span class="hljs-comment">// 42</span>
</code></pre>
<p>&#x6253;&#x5B8C;&#x5305;&#x540E;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span>(<span class="hljs-params">a, b</span>) </span>{
  <span class="hljs-keyword">return</span> a + b;
}

<span class="hljs-built_in">console</span>.log(add(<span class="hljs-number">16</span>, <span class="hljs-number">26</span>)); <span class="hljs-comment">// 42</span>
</code></pre>
<p>&#x4ECE;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;&#x6253;&#x5B8C;&#x5305;&#x540E;&#x5C06;&#x6240;&#x6709;&#x7684;js&#x90FD;&#x538B;&#x7F29;&#x5230;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x91CC;&#x4E86;&#x3002;&#x968F;&#x7740;&#x9879;&#x76EE;&#x8D8A;&#x6765;&#x8D8A;&#x5927;&#xFF0C;&#x6253;&#x5305;&#x7684;&#x6587;&#x4EF6;&#x4E5F;&#x4F1A;&#x8D8A;&#x6765;&#x8D8A;&#x5927;&#xFF0C;&#x5982;&#x679C;&#x518D;&#x5F15;&#x5165;&#x4E00;&#x4E9B;&#x7B2C;&#x4E09;&#x65B9;&#x7684;<code>js</code>&#x5E93;&#xFF0C;&#x90A3;&#x5C31;&#x66F4;&#x5E9E;&#x5927;&#x4E86;&#x3002;</p>
<p>&#x63A5;&#x4E0B;&#x6765;&#x4ECB;&#x7ECD;&#x4E00;&#x4E0B;&#x5982;&#x4F55;&#x5C06;<code>React</code>&#x4EE3;&#x7801;&#x5206;&#x9694;&#x3002;&#xFF08;&#x4EE5;&#x4E0B;&#x5185;&#x5BB9;&#x662F;16.6.0&#x7248;&#x672C;&#x624D;&#x652F;&#x6301;&#x7684;&#xFF09;</p>
<p><code>Code Splitting</code>&#x4F1A;&#x5E2E;&#x52A9;&#x4F60;&#x7684;&#x5E94;&#x7528;&#x5B9E;&#x73B0;<code>lazy load</code>.</p>
<p>&#x8FD9;&#x4E48;&#x505A;&#xFF0C;&#x5373;&#x4F7F;&#x6CA1;&#x6709;&#x51CF;&#x5C11;&#x6574;&#x4E2A;&#x9879;&#x76EE;&#x7684;&#x4EE3;&#x7801;&#x91CF;&#xFF0C;&#x4E5F;&#x4F1A;&#x907F;&#x514D;&#x5728;&#x9879;&#x76EE;&#x521D;&#x59CB;&#x52A0;&#x8F7D;&#x65F6;&#xFF0C;&#x52A0;&#x8F7D;&#x6CA1;&#x5FC5;&#x987B;&#x7684;<code>js</code>,&#x4ECE;&#x800C;&#x4F7F;&#x7528;&#x9879;&#x76EE;&#x6027;&#x80FD;&#x6709;&#x6240;&#x63D0;&#x5347;&#x3002;</p>
<h2 id="import">import()</h2>
<p>&#x6700;&#x7B80;&#x5355;&#x76F4;&#x63A5;&#x7684;&#x65B9;&#x5F0F;&#x5C31;&#x662F;&#x5F15;&#x5165;&#x52A8;&#x6001; <code>import</code> &#x5B9E;&#x73B0;&#x4EE3;&#x7801;&#x5206;&#x9694;&#x3002;</p>
<p>&#x4F7F;&#x7528; &#x52A8;&#x6001; <code>import</code> &#x4E4B;&#x524D;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { add } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./math&apos;</span>;

<span class="hljs-built_in">console</span>.log(add(<span class="hljs-number">16</span>, <span class="hljs-number">26</span>));
</code></pre>
<p>&#x4F7F;&#x7528;&#x52A8;&#x6001; <code>import</code> &#x540E;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span>(<span class="hljs-string">&quot;./math&quot;</span>).then(math =&gt; {
  <span class="hljs-built_in">console</span>.log(math.add(<span class="hljs-number">16</span>, <span class="hljs-number">26</span>));
});
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x52A8;&#x6001; <code>import</code> &#x5E76;&#x4E0D;&#x662F;&#x6807;&#x51C6;&#x7684;EcmaScript&#xFF0C;&#x6240;&#x4EE5;&#x9700;&#x8981;&#x914D;&#x7F6E; <a href="https://yarnpkg.com/en/package/babel-plugin-syntax-dynamic-import" target="_blank"><code>babel-plugin-syntax-dynamic-import</code></a></p>
</blockquote>
<h2 id="reactlazy">React.lazy</h2>
<p><code>React.lazy</code> &#x53EF;&#x4EE5;&#x4EE5;&#x4E00;&#x4E2A;React&#x6807;&#x51C6;&#x7EC4;&#x4EF6;&#x7684;&#x65B9;&#x6CD5;&#x6E32;&#x7136;&#x4E00;&#x4E2A;&#x52A8;&#x6001;&#x5F15;&#x5165;&#x7684;&#x7EC4;&#x4EF6;&#x3002;</p>
<p>&#x4EE5;&#x524D;&#x7684;&#x505A;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> OtherComponent <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./OtherComponent&apos;</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyComponent</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">OtherComponent</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  );
}
</span></code></pre>
<p>&#x4F7F;&#x7528; <code>React.lazy</code> &#xFF1A;</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> OtherComponent = React.lazy(() =&gt; <span class="hljs-keyword">import</span>(<span class="hljs-string">&apos;./OtherComponent&apos;</span>));

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyComponent</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">OtherComponent</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  );
}
</span></code></pre>
<p>&#x8FD9;&#x6837;&#x5728;&#x7EC4;&#x4EF6;<code>MyComponent</code>&#x6E32;&#x7136;&#x7684;&#x65F6;&#x5019;&#x624D;&#x52A0;&#x8F7D;<code>OtherComponent</code>&#x3002;</p>
<p><code>React.lazy</code> &#x5FC5;&#x987B;&#x4F7F;&#x7528;&#x52A8;&#x6001; <code>import()</code> &#x5F15;&#x5165;&#x7EC4;&#x4EF6;&#xFF0C;&#x5FC5;&#x987B;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; <code>Promise Component</code>&#x3002;</p>
<blockquote>
<p><code>React.lazy</code> &#x76EE;&#x524D;&#x4E0D;&#x652F;&#x6301;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x7136;</p>
</blockquote>
<h2 id="suspense">Suspense</h2>
<p>&#x5047;&#x5982;&#x5728; <code>React.lazy</code> &#x65F6;&#xFF0C;<code>import</code> &#x5931;&#x8D25;&#x6216;&#x8005;&#x5F02;&#x5E38;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x7ED9;&#x4E8E;&#x63D0;&#x793A;&#xFF0C;&#x6216;&#x8005;&#x4E00;&#x4E2A;&#x9ED8;&#x8BA4;&#x7684;&#x7EC4;&#x4EF6;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x9700;&#x8981;&#x4F7F;&#x7528;  <code>Suspense</code> .</p>
<p>&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> OtherComponent = React.lazy(() =&gt; <span class="hljs-keyword">import</span>(<span class="hljs-string">&apos;./OtherComponent&apos;</span>));

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyComponent</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Suspense</span> <span class="hljs-attr">fallback</span>=<span class="hljs-string">{&lt;div</span>&gt;</span>Loading...<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>}&gt;
        <span class="hljs-tag">&lt;<span class="hljs-name">OtherComponent</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Suspense</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  );
}
</span></code></pre>
<p><code>fallback</code> &#x4E5F;&#x662F;&#x4E00;&#x4E2A;&#x7EC4;&#x4EF6;&#xFF0C;&#x4F46;&#x4E0D;&#x80FD;&#x901A;&#x8FC7; &#x52A8;&#x6001; <code>import</code> &#x5F15;&#x5165;&#x7684;&#x7EC4;&#x4EF6;.</p>
<p><code>Suspense</code> &#x53EA;&#x80FD;&#x5305;&#x88F9;&#x4E00;&#x4E2A;&#x5355;&#x7ED3;&#x70B9;&#xFF0C;&#x5982;&#x679C;&#x6709;&#x591A;&#x4E2A; &#x52A8;&#x6001; <code>import</code> &#x7684;&#x7EC4;&#x4EF6;&#x9700;&#x8981;&#x653E;&#x5728;&#x4E00;&#x4E2A; <code>Suspense</code> &#x4E2D;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7C7B;&#x4F3C; <code>React.Fragmet</code> &#x5305;&#x88F9;&#x4E00;&#x4E0B;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5176;&#x4ED6;&#x7684; <code>React</code> &#x7EC4;&#x4EF6;&#x5305;&#x88F9;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> OtherComponent = React.lazy(() =&gt; <span class="hljs-keyword">import</span>(<span class="hljs-string">&apos;./OtherComponent&apos;</span>));
<span class="hljs-keyword">const</span> AnotherComponent = React.lazy(() =&gt; <span class="hljs-keyword">import</span>(<span class="hljs-string">&apos;./AnotherComponent&apos;</span>));

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyComponent</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    &lt;div&gt;
      &lt;Suspense fallback={&lt;div&gt;Loading...&lt;/div&gt;}&gt;
        &lt;section&gt;
          &lt;OtherComponent /&gt;
          &lt;AnotherComponent /&gt;
        &lt;/section&gt;
      &lt;/Suspense&gt;
    &lt;/div&gt;
  );
}
</code></pre>
<h2 id="&#x57FA;&#x4E8E;router&#x7684;&#x4EE3;&#x7801;&#x5206;&#x9694;">&#x57FA;&#x4E8E;Router&#x7684;&#x4EE3;&#x7801;&#x5206;&#x9694;</h2>
<p>&#x57FA;&#x4E8E; <code>Router</code> &#x7684;&#x4EE3;&#x7801;&#x5206;&#x9694;&#xFF0C;&#x4E5F;&#x662F;&#x6211;&#x4EEC;&#x901A;&#x5E38;&#x6240;&#x8BF4;&#x7684;&#x6309;&#x9700;&#x52A0;&#x8F7D;&#x3002;&#x662F;&#x6211;&#x4EEC;&#x63A8;&#x8350;&#x7684;&#x65B9;&#x5F0F;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Route, Switch } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> React, { Suspense, lazy } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;

<span class="hljs-keyword">const</span> Home = lazy(() =&gt; <span class="hljs-keyword">import</span>(<span class="hljs-string">&apos;./routes/Home&apos;</span>));
<span class="hljs-keyword">const</span> About = lazy(() =&gt; <span class="hljs-keyword">import</span>(<span class="hljs-string">&apos;./routes/About&apos;</span>));

<span class="hljs-keyword">const</span> App = () =&gt; (
  &lt;Router&gt;
    &lt;Suspense fallback={&lt;div&gt;Loading...&lt;/div&gt;}&gt;
      &lt;Switch&gt;
        &lt;Route exact path=&quot;/&quot; component={Home}/&gt;
        &lt;Route path=&quot;/about&quot; component={About}/&gt;
      &lt;/Switch&gt;
    &lt;/Suspense&gt;
  &lt;/Router&gt;
);
</code></pre>
<h2 id="named-exports">Named Exports</h2>
<p><code>React.lazy</code> &#x76EE;&#x524D;&#x53EA;&#x652F;&#x6301; <code>default</code> &#x5BFC;&#x51FA;&#xFF0C;&#x4E0D;&#x652F;&#x6301;&#x547D;&#x540D;&#x5BFC;&#x51FA;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x53EA;&#x652F;&#x6301;&#xFF1A;</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> () =&gt; {
  <span class="hljs-keyword">return</span>(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>I am a Lazy component<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>);
}
</code></pre>
<p>&#x5982;&#x679C;&#x8981;&#x652F;&#x6301;&#x547D;&#x4EE4;&#x5BFC;&#x51FA;&#xFF0C;&#x9700;&#x8981;&#x91CD;&#x65B0;&#x518D; <code>export</code> ,&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-jsx"><span class="hljs-comment">// ManyComponents.js</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> MyComponent = <span class="hljs-comment">/* ... */</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> MyUnusedComponent = <span class="hljs-comment">/* ... */</span>;
</code></pre>
<pre><code class="lang-jsx"><span class="hljs-comment">// MyComponent.js</span>
<span class="hljs-keyword">export</span> { MyComponent <span class="hljs-keyword">as</span> <span class="hljs-keyword">default</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;./ManyComponents.js&quot;</span>;
</code></pre>
<pre><code class="lang-jsx"><span class="hljs-comment">// MyApp.js</span>
<span class="hljs-keyword">import</span> React, { lazy } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">const</span> MyComponent = lazy(() =&gt; <span class="hljs-keyword">import</span>(<span class="hljs-string">&quot;./MyComponent.js&quot;</span>));
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="07.html" class="navigation navigation-prev " aria-label="Previous page: 类型查检">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../chapter06/00.html" class="navigation navigation-next " aria-label="Next page: React 实例">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"代码分隔","level":"1.6.8","depth":2,"next":{"title":"React 实例","level":"1.7","depth":1,"path":"chapter06/00.md","ref":"./chapter06/00.md","articles":[{"title":"List","level":"1.7.1","depth":2,"path":"chapter06/01.md","ref":"./chapter06/01.md","articles":[]},{"title":"Form","level":"1.7.2","depth":2,"path":"chapter06/02.md","ref":"./chapter06/02.md","articles":[]},{"title":"Form高级","level":"1.7.3","depth":2,"path":"chapter06/03.md","ref":"./chapter06/03.md","articles":[]},{"title":"Tooltip","level":"1.7.4","depth":2,"path":"chapter06/04.md","ref":"./chapter06/04.md","articles":[]}]},"previous":{"title":"类型查检","level":"1.6.7","depth":2,"path":"chapter05/07.md","ref":"./chapter05/07.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"root":"./books","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"小翼","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"React 基础学习资料","language":"zh-hans","links":{"gitbook":false,"sharing":{"google":true,"facebook":true,"twitter":true}},"gitbook":"*"},"file":{"path":"chapter05/08.md","mtime":"2020-03-07T07:33:43.864Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-03-09T08:08:41.080Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

